import { isEmpty } from 'shiptools'
import { checkBelongTypeApi  } from '../../api/index'
import { getAgeInt } from '../../utils/index'

export function ticketTypeBuilder({ passengerTypeConf,
    seat,
    p,
    supportChild, 
    supportOld
}) {
    
    let ticketTypeChoose = false 
    // let ticketTypePickerPassengers = []
    if(!isEmpty(passengerTypeConf)) {
      const oldAgeConf = passengerTypeConf.OldAge
      const oldHeightConf = passengerTypeConf.OldHeight
      
      const childAgeConf = passengerTypeConf.ChildAge
      const childHeightConf = passengerTypeConf.ChildHeight
      const age = getAgeInt(p.birthday)
      
      let validChildAgeRange = false
      let ticketTypeDesc = []
      let _ticketTypePickerPassengers = []
      
      p.linkerType = 0
      if (oldAgeConf || childAgeConf) {
          ticketTypeDesc.push('年龄')
      }
      if (passengerTypeConf.ChildHeight) {
          ticketTypeDesc.push('身高')
      }
      // 1.优先长者票（长者票只会有年龄配置）
      // 2.儿童配置年龄、常旅符合年龄范围，则默认选择为儿童票； 儿童票配置年龄和身高（符合年龄要求，因为有身高则弹picker，不符合年龄要求，即使有身高配置也不弹框）
      /** 儿童 **\
      \**     **/
      if(supportChild) {
          if(childHeightConf) {
              if(childAgeConf) {
                  validChildAgeRange = age <= childAgeConf.MaxVal && childAgeConf.MinVal <= age
              }
              if((childAgeConf && validChildAgeRange) || !childAgeConf) {
                  ticketTypeChoose = true
                  let childDesc = childAgeConf && [childAgeConf.Content] || []
                  childDesc.push(childHeightConf.Content)
                  _ticketTypePickerPassengers.push({
                      id: 2,
                      type: 1,
                      desc: childDesc,
                      price: seat.ChildSeatPrice,
                      seat: seat.DisplayName,
                      isSelected: false
                  })
              }
  
              if(validChildAgeRange) {
                  p.linkerType = 1
                  if(seat.CitizenChildSeatPrice > 0) {
                      _ticketTypePickerPassengers.push({
                          id: 24,
                          type: 24,
                          desc: [],
                          price: seat.CitizenChildSeatPrice,
                          seat: seat.DisplayName,
                          isSelected: false
                      })
                  }
              }
          } else if(childAgeConf) {
              validChildAgeRange = age <= childAgeConf.MaxVal && childAgeConf.MinVal <= age
              if(validChildAgeRange) {
                  p.linkerType = 1
                  
                  if(seat.CitizenChildSeatPrice > 0) {
                      ticketTypeChoose = true
                      const childDesc = childAgeConf && [childAgeConf.Content] || []
                      _ticketTypePickerPassengers.push({
                          id: 2,
                          type: 1,
                          desc: childDesc,
                          price: seat.ChildSeatPrice,
                          seat: seat.DisplayName,
                          isSelected: false
                      })
                      
                      _ticketTypePickerPassengers.push({
                          id: 3,
                          type: 24,
                          desc: childDesc,
                          price: seat.CitizenChildSeatPrice,
                          seat: seat.DisplayName,
                          isSelected: false
                      })
                  }
              }
          }
      }
      
      /** 长者 **\
      \**     **/
      if(supportOld) {
          let validOldAgeRange = false
          if(oldHeightConf) {
              if(oldAgeConf) {
                  validOldAgeRange = age <= oldAgeConf.MaxVal && oldAgeConf.MinVal <= age
              }
              if((oldAgeConf && validOldAgeRange) || !oldAgeConf) {
                  ticketTypeChoose = true
                  let oldDesc = [oldAgeConf.Content]
                  oldDesc.push(oldHeightConf.Content)
                  _ticketTypePickerPassengers.push({
                      id: 3,
                      type: 3,
                      desc: oldDesc,
                      price: seat.OldSeatPrice,
                      seat: seat.DisplayName,
                      isSelected: false
                  })
  
                  if(validOldAgeRange) {
                      p.linkerType = 3
                  }
              }
          } else if(oldAgeConf) {
              validOldAgeRange = age <= oldAgeConf.MaxVal && oldAgeConf.MinVal <= age
              if(validOldAgeRange) {
                  p.linkerType = 3
              }
          }
      }
      
      if(!validChildAgeRange || childHeightConf) {
          _ticketTypePickerPassengers.push({
              id: 0,
              type: 0,
              desc: [],
              price: seat.SeatPrice,
              seat: seat.DisplayName,
              isSelected: false
          })
      }
      
      // 北海市民成人
      if(seat.CitizenSeatPrice > 0 && (!validChildAgeRange || childHeightConf)) {
          ticketTypeChoose = true
          _ticketTypePickerPassengers.push({
              id: 1,
              type: 22,
              desc: ['限北海市民'],
              price: seat.CitizenSeatPrice,
              seat: seat.DisplayName,
              isSelected: false
          })
      }
  
      _ticketTypePickerPassengers.forEach(item => {
        item.isBeihai = [1, 24, 3].includes(item.type)
      })
  
      _ticketTypePickerPassengers.sort((a, b) => a.id - b.id)
      const ticketDesc = ticketTypeDesc.join('、')
  
      const ticketTypePickerPassengers = {
        name: p.showName,
        passenger: p,
        ticketTypeDesc: ticketDesc,
        title: `请根据${p.showName}${ticketDesc},选择票型`,
        list: _ticketTypePickerPassengers
      }
        //   this.setData({
        //       ticketTypePickerPassengers
        //   })
      return {
        ticketTypePickerPassengers, 
        ticketTypeChoose,
        passenger: p
      }
    }

    
    return {
        ticketTypePickerPassengers: {
            name: p.showName,
            passenger: p,
            ticketTypeDesc: '',
            title: ``,
            list: []
        }, 
        ticketTypeChoose: false,
        passenger: p
    }
}

export function checkBeiHaiTicket(params, linkerType) {
    return checkBelongTypeApi(params).then(response => {
        if(!response) {
          let errMsg = '校验失败，请稍后再试'
          switch(linkerType) {
              case 0:
                  errMsg = `该乘客不满足票型要求：中高风险地区，请至线下购票`
                  break;
              case 1:
                  errMsg = `该乘客不满足票型年龄要求`
                  break;
              case 22:
                  errMsg = `该乘客不满足票型要求：非北海市民`
                  break;
              case 24:
                  errMsg = `该乘客不满足票型年龄要求，且非北海市民`
                  break;
              default:
                  break;
          }
          // 1.儿童：
          // 提示：该乘客不满足票型年龄要求
          // 2.北海儿童：
          // 提示：该乘客不满足票型年龄要求，且非北海市民
          // 3.成人：
          // 提示：该乘客不满足票型要求：中高风险地区，请至线下购票
          // 2.北海成人：
          // 提示：该乘客不满足票型要求：非北海市民
          wx.showToast({
              title: errMsg,
              icon: 'none',
              duration: 2000
          })
          return
        }
        
        return response

      })
}